{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建某个文件\n",
    "import os\n",
    "\n",
    "os.mkdir(r\"/home/colby/tmp/python\")\n",
    "\n",
    "content = '''我是文件的内容\n",
    "内容是：\n",
    "如何做好　Python 量化投资\n",
    "'''\n",
    "\n",
    "file = open(r\"/home/colby/tmp/python/test.txt\", 'w') # w 参数，　直接打开一个文件，　不存在则创建文件\n",
    "\n",
    "file.write(content)\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是文件的内容\n",
      "\n",
      "内容是：\n",
      "\n",
      "如何做好　Python 量化投资\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 读取文件内容\n",
    "fr = open(r\"/home/colby/tmp/python/test.txt\")\n",
    "while True:\n",
    "    line = fr.readline()\n",
    "    if len(line) == 0 :\n",
    "        break\n",
    "    print(line)\n",
    "fr.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是文件的内容\n",
      "\n",
      "内容是：\n",
      "\n",
      "如何做好　Python 量化投资\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 另外一种，读取文件内容的方法\n",
    "\n",
    "frr = open(r\"/home/colby/tmp/python/test.txt\")\n",
    "\n",
    "for u in frr:\n",
    "    print(u)\n",
    "fr.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '/home/colby/tmp/python/test.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-11-688c10d85444>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetcwd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 获取当前的工作目录\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mr\"/home/colby/tmp/python/test.txt\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 用于删除一个文件。\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/home/colby/tmp/python/test.txt'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "os.getcwd() # 获取当前的工作目录\n",
    "\n",
    "os.remove(r\"/home/colby/tmp/python/test.txt\") # 用于删除一个文件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
